/* ----------------------------------------------------------------------
 *	Shadow Quest
 *	Copyright (C) 2009 Prageeth Silva <prageeth@thenewcoders.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  ----------------------------------------------------------------------
 */

package units.villagers;

import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import sq.RPG;
import units.Player;
import units.Unit;
import map.Item;
import map.World;

/** Represents the Requester characters
 * 
 */
public class Requester extends Villager
{
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** Create a new Villager object (overload).
	 * @param type The type of the villager
	 * @param x The x coordinate in of the villager
	 * @param y The y coordinate in of the villager
	 * @param maxHP The maximum HP of the villager
	 * @throws SlickException
	 */
    public Requester(World world, int team)
    throws SlickException
    {
    	// temporarily make the super class and then replace the images
    	super(world, team);
       	this.setKillable(false);
    	
       	this.setDirectoryPath("assets/units/villagers/prince/");
		this.setName("Prince Aldric");
    	this.setAttackType(AttackType.UNATTACKABLE);
    	this.finalizeLoading();  	
    	this.standInRandomDirection();
    	
    }
    
    public Requester(World world)
    throws SlickException
    {
    	this(world, 0);
    }
    
    /** Update the player's state for a frame.
     * @param world A reference to the world.
     * @param input The input object, used to take the keyboard inputs.
     * @param delta Time passed since last frame (milliseconds).
     */
    @Override
    public void update(Input input, int delta) {
    
    	if (this.isAlive()) {
    		
	     	Player player = this.getWorld().getPlayer();
	     	double dist2 = calculateDistanceSqrd(player.getX(), player.getY(), this.getX(), this.getY());
	    	
	     	if (!player.isTeleporting() && player.isAlive() && (dist2 <= Unit.Squared(CLOSE_PROXIMITY))) {
					Item i = Item.searchItem(player,Item.ItemType.ELIXIR);
					if ((i != null) || (Item.searchItem(this,Item.ItemType.ELIXIR) != null)) {
						this.setDialogue("The elixir! My father is cured! Thankyou!");
						if (i != null) { player.giveItem(i,this); }
						this.getWorld().getQuest().winQuest();
					} else {
						this.setDialogue("Please seek out the Elixir of Life to cure the king.");
					}
					if (!this.isDialogueShown()) { this.showDialogue(); }
					this.setDialogueShown(true);	
			} else {
					this.setDialogueShown(false);
			}
				
    	}
			
    	super.update(input, delta);
        
    }
    
}
